/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package GameLogic;

import java.util.*;
import Enums.Direction;
import Settings.*;

/**
 *
 * @author RikuXan + Mastre
 */
public class Snake
{
    //Die Bestandteile der Schlange
    private Vector <Element> elements;
    //Die Blickrichtung der Schlange
    private Direction orientation;
    //Das Aussehen der Schlange; Name der Datei
    private String design = "snake.bmp";
    //Länge der Schlange
    private int snakeLength = 0;


    public Snake()
    {
        elements = new Vector<Element>();
        elements.add( new Head( 0 , 0 ) );
        orientation = Direction.NORTH;
    }

    public void addElement( int x , int y )
    {
        elements.add( new Body( x , y ) );
    }

    public boolean ElementInstanceof( int pos , Class c )
    {
        return ( c.isInstance( elements.elementAt(pos) ) );
    }

    public void setSnakeLength( int length )
    {
        snakeLength = length;
        
        while( getElementCount() > snakeLength )
            elements.remove( getElementCount() -1 );
        
        while( getElementCount() < snakeLength )
            addElement( elements.lastElement().getPosX() , elements.lastElement().getPosY() );

    }
    
    public void reset()
    {
        elements.clear();
        snakeLength = Settings.startLength;
        elements.add( new Head( 0 , 0 ) );
    }

    public void eat()
    {
        setSnakeLength(snakeLength+1);
    }

    public int getSnakeLength()
    {
        return snakeLength;
    }

    //Bewegung der Schlange in eine Richtung
    public void move()
    {
        //Das letzte Element rückt an die Stelle des Vorletzten usw.
        for( int i = elements.size() - 1; i > 0 ; i-- )
        {
            elements.elementAt(i).setPosX( elements.elementAt(i-1).getPosX() );
            elements.elementAt(i).setPosY( elements.elementAt(i-1).getPosY() );
        }
        //Das erste bewegt sich ein Feld in Blickrichtung
        ( (Head)elements.firstElement() ).move( orientation );

    }

    public void setHeadCoords( int x , int y )
    {
        elements.firstElement().setPosX(x);
        elements.firstElement().setPosY(y);
    }

    public Direction getOrientation()
    {
        return orientation;
    }

    public int getElementCount()
    {
        return elements.size();
    }
    
    public String getDesign()
    {
        return design;
    }

    public int getPosX( int position )
    {
        return elements.elementAt(position).getPosX();
    }

    public int getPosY( int position )
    {
        return elements.elementAt(position).getPosY();
    }

    public void setOrientation( Direction newOrientation )
    {
        if(getElementCount() > 1)
        {
            if( !( elements.firstElement().compare( elements.elementAt(1) ) == newOrientation ) )
            {
                orientation = newOrientation;
            }
        }
        else
            orientation = newOrientation;
    }

    public void setDesign( String newDesign )
    {
        design = newDesign;
    }

    public String getTexture( int position )
    {
        return elements.elementAt( position ).getTexture();
    }

}